'use strict';
/**
 * @name 技术方案list页的controller
 * @author gu
 */
angular.module('metalabPartnerApp').controller('TechnologyListCtrl', ['$scope', '$state', '$stateParams', 'MetaResources', 'MetaConstants', 'MetaRemind',
    function ($scope, $state, $stateParams, resource, MetaConstants, MetaRemind) {
        $scope.myInterval = MetaConstants.slideTime;
        $scope.searchText;
        $scope.filters = [];

        /**
         * @function 发送广播筛选器选中项
         * @param a 选中项名
         */
        // var afterSelect = function (a) {
        //     $scope.$broadcast('afterSelect', {
        //         item: a
        //     });
        // };

        $scope.tabs = [
            {
                name: '所有方案',
                active: true,
                select: function () {
                    $scope.searchText = $scope.searchText || '';
                    $state.go('meta.technology.list.all');
                }
            }, {
                name: '我发布的方案',
                active: false,
                select: function () {
                    $scope.searchText = '';
                    $state.go('meta.technology.list.released');
                }
            }, {
                name: '我收藏的方案',
                active: false,
                select: function () {
                    $scope.searchText = '';
                    $state.go('meta.technology.list.favorite');
                }
            }
        ];

        $scope.filters = [{
            title: '应用领域',
            items: ['化学化工', '能源环保', '生农医药', '机械电子', '材料科学', '其他'],
            config: {
                chose: '不限',
                showNumber: 0
            }
        }, {
            title: '应用效果',
            items: [],
            config: {
                chose: '不限',
                showNumber: 0
            }
        }, {
            title: '技术来源',
            items: ['中国', '国外'],
            // items: origins && origins.length > 0 ? ['中国', '国外'].concat(angular.copy(origins)) : [],
            config: {
                chose: '不限',
                showNumber: 0
            }
        }];

        ///**搜索
        // $scope.tags = ['生态治理', '节能工程', '高温合金', '能源材料', '改性材料', '智能制造'];


        resource.effectTages.get({}, function (tags) {
            // resource.technology.origin({}, function (origins) {
            //     origins.splice(origins.indexOf('中国'), 1);
            $scope.filters[1].items = tags && tags.length > 0 ? angular.copy(tags) : [];
            // }, function () {
            // });
        }, function () {
        });


        /**
         * @function 标签选择
         * @param item 选中的标签
         */
        $scope.selectTag = function (item) {
            $scope.searchText = item;
            $scope.tagChose = $scope.tagChose === item ? null : item;
            $state.go("meta.technology.list.all", {
                page: 1,
                keywords: $scope.tagChose,
                area: $scope.filters[0].config.chose == '不限' ? null : $scope.filters[0].config.chose,
                effect: $scope.filters[1].config.chose == '不限' ? null : $scope.filters[1].config.chose,
                origin: $scope.filters[2].config.chose == '不限' ? null : $scope.filters[2].config.chose
            });
        };

        /**
         * @function 监听筛选
         */
        $scope.$watch("filters[0].config.chose", function (newVal, oldVal) {
            if (newVal !== oldVal) {
                $state.go('.', {
                    page: '1',
                    area: newVal === '不限' ? null : newVal,
                    effect: $scope.filters[1].config.chose == '不限' ? null : $scope.filters[1].config.chose,
                    origin: $scope.filters[2].config.chose == '不限' ? null : $scope.filters[2].config.chose,
                    keywords: $scope.searchText
                });
            }
        });
        $scope.$watch("filters[1].config.chose", function (newVal, oldVal) {
            if (newVal !== oldVal) {
                $state.go(".", {
                    page: '1',
                    effect: newVal === '不限' ? null : newVal,
                    area: $scope.filters[0].config.chose == '不限' ? null : $scope.filters[0].config.chose,
                    origin: $scope.filters[2].config.chose == '不限' ? null : $scope.filters[2].config.chose,
                    keywords: $scope.searchText
                });
            }
        });
        $scope.$watch("filters[2].config.chose", function (newVal, oldVal) {
            if (newVal !== oldVal) {
                $state.go('.', {
                    page: '1',
                    origin: newVal === '不限' ? null : newVal,
                    effect: $scope.filters[1].config.chose == '不限' ? null : $scope.filters[1].config.chose,
                    area: $scope.filters[0].config.chose == '不限' ? null : $scope.filters[0].config.chose,
                    keywords: $scope.searchText
                });
            }
        });

        /**
         * @function [搜索功能]
         */
        $scope.search = function () {
            // if ($scope.searchText && $scope.searchText.length > 0) {
            $state.go("meta.technology.list.all", {
                page: 1,
                area: $stateParams.area,
                effect: $stateParams.effect,
                origin: $stateParams.origin,
                keywords: $scope.searchText
            });
            // } else {
            //     MetaRemind.warn('请输入关键字进行搜索');
            // }
        };
    }]);
